package com.tdkj.service.information.impl;

import java.sql.Timestamp;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tdkj.dao.information.IBeanInfoCommentDao;
import com.tdkj.dao.information.IBeanBaseInfoDao;
import com.tdkj.dao.information.IBeanInfoModuleDao;
import com.tdkj.dao.user.IBeanArticleReplyDao;
import com.tdkj.dao.user.IBeanMessageDao;
import com.tdkj.dao.user.IBeanUserDao;
import com.tdkj.dao.user.IBeanUserLevelDao;
import com.tdkj.model.base.BaseEntity;
import com.tdkj.model.information.BeanInfoComment;
import com.tdkj.model.information.BeanInfoReply;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanInfoModule;
import com.tdkj.model.information.BeanModuleColumn;
import com.tdkj.model.user.BeanFriend;
import com.tdkj.model.user.BeanMessage;
import com.tdkj.model.user.BeanUser;
import com.tdkj.model.user.BeanUserLevel;
import com.tdkj.service.BaseService;
import com.tdkj.service.information.IBeanBaseInfoService;
import com.tdkj.util.md5.MD5Util;
@Component("beanBaseInfoService")
public class BeanBaseInfoServiceImpl extends BaseService implements IBeanBaseInfoService{
	private IBeanBaseInfoDao beanBaseInfoDao;
	private IBeanInfoModuleDao beanInfoModuleDao;
	private IBeanUserDao beanUserDao;
	private IBeanUserLevelDao userLevelDao;
	private IBeanInfoCommentDao beanArticleCommentDao;
	private IBeanMessageDao messageDao; 
	private IBeanArticleReplyDao replyDao;
	/**
	 * @param beanBaseInfoDao the beanBaseInfoDao to set
	 */
	@Resource
	public void setBeanBaseInfoDao(IBeanBaseInfoDao beanBaseInfoDao) {
		this.beanBaseInfoDao = beanBaseInfoDao;
	}
	
	@Resource
	public void setBeanInfoModuleDao(IBeanInfoModuleDao beanInfoModuleDao) {
		this.beanInfoModuleDao = beanInfoModuleDao;
	}

	@Resource
	public void setBeanUserDao(IBeanUserDao beanUserDao) {
		this.beanUserDao = beanUserDao;
	}
	@Resource
	public void setBeanArticleCommentDao(IBeanInfoCommentDao beanArticleCommentDao) {
		this.beanArticleCommentDao = beanArticleCommentDao;
	}
	@Resource
	public void setUserLevelDao(IBeanUserLevelDao userLevelDao) {
		this.userLevelDao = userLevelDao;
	}
	@Resource
	public void setMessageDao(IBeanMessageDao messageDao) {
		this.messageDao = messageDao;
	}
	@Resource
	public void setReplyDao(IBeanArticleReplyDao replyDao) {
		this.replyDao = replyDao;
	}

	@Override
	public BeanBaseInfo createBaseInfo(String title,
			Map<String, String> properties) {
		BeanBaseInfo bean = new BeanBaseInfo();
		bean.setTitle(title);
		bean.setProp(properties);
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		beanBaseInfoDao.save(bean);
		return bean;
	}
	
	@Override
	public void deleteBaseInfo(long id) {
		BeanBaseInfo bean = beanBaseInfoDao.findById(id, BeanBaseInfo.class);
		beanBaseInfoDao.delete(bean);
	}
	
	@Override
	public void updateBaseInfo(long id, String title, Map<String, String> properties) {
		BeanBaseInfo bean = beanBaseInfoDao.findById(id, BeanBaseInfo.class);
		bean.setTitle(title);
		bean.setProp(properties);
	}

	@Override
	public BeanBaseInfo getBaseInfo(long id) {
		return beanBaseInfoDao.findById(id, BeanBaseInfo.class);
	}

	@Override
	public List<BeanModuleColumn> getColumns(String moduletype) {
		List<BeanInfoModule> modules=beanInfoModuleDao.findByProperty("moduletype", moduletype, BeanInfoModule.class);
		if(modules.size()==1){
			return modules.get(0).getModuleColumns();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BeanBaseInfo> getInfos(String moduletype, String columntype) {
		BeanBaseInfo example = new BeanBaseInfo();
		example.setModuletype(moduletype);
		example.setColumntype(columntype);
		List<BeanBaseInfo> infos = beanInfoModuleDao.findByExample(example);
		return infos;
	}

	@Override
	public BeanBaseInfo getClickTime(Long id) {
		// TODO Auto-generated method stub
		BeanBaseInfo bean=beanBaseInfoDao.findById(id, BeanBaseInfo.class);
		bean.setClicktime(bean.getClicktime()+1);
		return bean;
	}

	@Override
	public BeanBaseInfo saveComment(Long id,String content,BeanUser user) {
		// TODO Auto-generated method stub
		BeanBaseInfo bean = beanBaseInfoDao.findById(id, BeanBaseInfo.class);
		//BeanUser u =  beanUserDao.findById(1L, BeanUser.class);
		BeanInfoComment comment=new BeanInfoComment();
		comment.setCreatetime(new Timestamp(System.currentTimeMillis()));
		comment.setContent(content);
		comment.setUser(user);
		comment.setBaseinfo(bean);
		beanArticleCommentDao.save(comment);
		bean.getInfoComments().add(comment);
		return bean;
	}

	@Override
	public void publishBaseInfo(long id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addFriend(Long uId, Long fId) {
		// TODO Auto-generated method stub
		BeanUser u =  beanUserDao.findById(uId, BeanUser.class);
		BeanUser f =  beanUserDao.findById(fId, BeanUser.class);
		
	}

	@Override
	public void sendMsg2Friend(BeanMessage msg){
		messageDao.save(msg);
	}

	@Override
	public void replyCommment(Long uId,Long cId,BeanInfoReply reply) {
		// TODO Auto-generated method stub
		BeanInfoComment c=beanArticleCommentDao.findById(cId, BeanInfoComment.class);
		BeanUser u = beanUserDao.findById(uId, BeanUser.class);
		reply.setUser(u);
		List<BeanInfoReply> list=c.getReplys();
		list.add(reply);
		reply.setComment(c);
		//beanArticleCommentDao.save(c);
		replyDao.save(reply);
	}

	@Override
	public List<BeanBaseInfo> setStatus(Long id) {
		// TODO Auto-generated method stub
		BeanBaseInfo  bean = beanBaseInfoDao.findById(id, BeanBaseInfo.class);
		bean.setStatus("已审核");
		beanBaseInfoDao.merge(bean);
		BeanBaseInfo t=new BeanBaseInfo();
		t.setStatus("未审核");
		return beanBaseInfoDao.findByExample(t);
	}
}
